package com.bgs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bgs.mapper.DetailedMapper;
import com.bgs.pojo.Detailed;
import com.bgs.pojo.Social;
import com.bgs.service.DetailedService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bgs.service.SocialService;
import com.bgs.utils.LocaldatetimeUtils;
import com.bgs.vo.SocialVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 社保明细表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-05-08
 */
@Service
public class DetailedServiceImpl extends ServiceImpl<DetailedMapper, Detailed> implements DetailedService {

    @Autowired
    private SocialService socialService;

    @Override
    public List<Detailed> findAllDetailed1(Integer sid) {
        QueryWrapper<Detailed> w1 = new QueryWrapper<>();
        w1.eq("sid",sid);//关联社保表id
        w1.eq("type",1);//社保明细 1是社保明细
        List<Detailed> list1 = this.list(w1);
        Double ltotalp = 0D;//个人金额缴纳合计
        Double ltotalg = 0D;//公司金额缴纳合计
        Double total1 = 0D;//总合计
        for (Detailed d : list1) {
            ltotalp += d.getPerson();
            ltotalg +=d.getCompany();
        }
        total1 = ltotalg+ltotalp;

        Detailed d1 = new Detailed();
        d1.setInsurance("合计");
        d1.setPerson(ltotalp);
        d1.setCompany(ltotalg);
        d1.setTotal(total1);

        list1.add(d1);
        return list1;
    }

    @Override
    public List<Detailed> findAllDetailed2(Integer sid) {

        QueryWrapper<Detailed> w2 = new QueryWrapper<>();
        w2.eq("sid",sid);//关联社保表id
        w2.eq("type",2);//社保明细 2 是公积金明细
        Double total2 = 0D;//总合计
        List<Detailed> list2 = this.list(w2);
        Double ltotalps = 0D;//个人金额缴纳合计
        Double ltotalgs = 0D;//公司金额缴纳合计
        for (Detailed d : list2) {
            ltotalps += d.getPerson();
            ltotalgs +=d.getCompany();
        }
        total2 = ltotalps+ltotalgs;

        Detailed d2 = new Detailed();
        d2.setInsurance("合计");
        d2.setPerson(ltotalps);
        d2.setCompany(ltotalgs);
        d2.setTotal(total2);

        list2.add(d2);
        return list2;
    }

    @Override
    public SocialVo findByIdSpcialvo(Integer sid) {
        Social m = socialService.selectByPrimaryKey(sid);
        SocialVo s = new SocialVo();
        BeanUtils.copyProperties(m,s);
        s.setTime(LocaldatetimeUtils.timess(m.getPayment()));
        return s;
    }

    @Override
    public boolean updateDetailed(Detailed d) {
        return baseMapper.updateDetailed(d);
    }
}
